package com.crawler.gmail.controller;

/**
 * <描述>
 * <详细介绍>
 *
 * @author 王明明
 * @since wiki
 */

import com.crawler.gmail.crawler.EmailCrawler;
import com.crawler.gmail.crawler.EmailFetchSettingDO;
import com.crawler.gmail.model.EmailFetchSettingParam;
import com.crawler.gmail.model.RestJson;
import com.crawler.gmail.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.Store;

@RestController
public class HelloController {
    private final static Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping(value = "/verify-gmail-info", method = RequestMethod.POST)
//    public String index(String email, String password, Integer protocol, String host, Integer port, Integer ssl) {
    public String index(@RequestBody EmailFetchSettingParam model) {
        String email = model.getEmail();
        String password = model.getPassword();
        Integer protocol = model.getProtocol();
        String host = model.getHostname();
        Integer port = model.getPort();
        boolean ssl = model.getSsl() == 1;
        System.out.println(JsonUtil.toJson(model));
        LOGGER.info("验证gmail邮箱 {} {} {}", email, password, protocol);
        EmailFetchSettingDO emailFetchSettingDO = new EmailFetchSettingDO();
        emailFetchSettingDO.setUsername(email);
        emailFetchSettingDO.setHost(host);
        emailFetchSettingDO.setPort(port);
        emailFetchSettingDO.setProtocol(protocol);
        emailFetchSettingDO.setPassword(password);
        emailFetchSettingDO.setSsl(ssl);
        try {
            EmailCrawler emailCrawler = new EmailCrawler(emailFetchSettingDO);
            Store store = emailCrawler.getJavaMailInstance(emailFetchSettingDO);
            if (store == null){
                LOGGER.info("{} 验证失败", email);
                System.out.println(email + " error");
                return JsonUtil.toJson(new RestJson(1, ""));
            }
            System.out.println(email + " ok");
            LOGGER.info("{} 验证成功", email);
            Thread t = new Thread(new Runnable(){
                public void run(){
                    // run方法具体重写
                    try {
                        emailCrawler.fetchAllEmail(store);
                        LOGGER.info("邮件抓取完成 {}", emailFetchSettingDO.getUsername());
                    } catch (MessagingException e) {
                        LOGGER.error("邮件抓取出错 " + emailFetchSettingDO.getUsername(), e);
                    }
                }});
            t.start();
            return JsonUtil.toJson(new RestJson(0, "验证成功"));
        }catch (Exception e){
            e.printStackTrace();
            String msg = e.getMessage();
            if (msg.contains("Invalid credentials")){
                msg = "账号或密码错误.";
            }
            if (msg.contains("Application-specific password required")){
                msg = "请用应用专用密码登录.";
            }
            LOGGER.info(email + " 验证出错", e);
            return JsonUtil.toJson(new RestJson(1, msg));
        }
    }
}
